<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    :title="getTitle"
    width="800px"
    @ok="handleSubmit"
    destroyOnClose
  >
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup>
import { ref, computed, unref, useAttrs } from "vue";
import { BasicForm, useForm } from "/src/components/Form";
import { BasicDrawer, useDrawerInner } from "/src/components/Drawer";
import { BasicTree, TreeItem } from "/src/components/Tree";
import { formSchema } from "./index.data";
import { save } from "./index.api";
import { useSuccess } from "/@/hooks/rw/handleSuccess";
const { rhandleSuccess } = useSuccess();
const info = ref({});
// 声明Emits
const emit = defineEmits(["success", "register"]);
const attrs = useAttrs();
const isUpdate = ref(true);
const [registerForm, { setProps, resetFields, setFieldsValue, validate }] = useForm({
  labelWidth: 90,
  schemas: formSchema,
  showActionButtonGroup: false,
  autoFocusFirstItem:true,
});
const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  resetFields();
  isUpdate.value = !!data?.isUpdate;
  data = JSON.parse(JSON.stringify(data));
  data.record.proSpec = data.record.proSpecId;
  info.value = data.record;
  // data.record.suppliesId = data.record.suppliesNoId
  setDrawerProps({ confirmLoading: false });
  if (unref(isUpdate)) {
    setFieldsValue({
      ...data.record,
    });
  }
  //禁用表单
  setProps({ disabled: !attrs.showFooter });
});
/**
 * 标题
 */
const getTitle = computed(() => (!unref(isUpdate) ? "添加发票" : "编辑发票"));
/**
 * 提交
 */
async function handleSubmit() {
  try {
		const values = await validate();
    let res;
    let data = {
      ...info.value,
      ...values,
    };
    data.proBatchNo = data.proBatchNo || " ";
    data.proDesc = data.proDesc || " ";
    setDrawerProps({ confirmLoading: true });
    //提交表单
    res = await save(data);
    rhandleSuccess(res, () => {});
    closeDrawer();
    emit("success");
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}
</script>
<style lang="less" scoped>
.scrollbar__view {
  .ant-form {
    width: 700px;
    margin-top: 10px;
  }
}
.imgUpText {
  position: absolute;
  top: 170px;
  right: 105px;
  font-size: 15px;
  color: rgb(22, 155, 213);
}
.imgBotText {
  position: absolute;
  top: 190px;
  right: 35px;
  color: #999;
  font-size: 12px;
}
</style>
